package com.ltmall.api.wechat;

import com.ltmall.api.controller.WechatController;
import com.ltmall.api.util.MessageUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class EventDispatcher {
    private static Logger logger = LogManager.getLogger(WechatController.class);

    public static String processEvent(Map<String, String> map) {
        //关注事件
        if (map.get("Event").equals(MessageUtil.EVENT_TYPE_SUBSCRIBE)) {
            //自动回复
            /*String autoReply = "欢迎来到这里，\n"
                    + "和喜爱家乡美食的老乡们，\n"
                    + "一起找回家的味道\n"
                    + "\n"
                    + "/玫瑰迎中秋，送大礼  <a href=\"http://mp.weixin.qq.com/s/kE9N-DkMKGR4jiqPJaMiew\">点击了解详情>></a>\n"
                    + "/玫瑰领董事特权，享平价抵扣 <a href=\"http://www.xiangweihui.com/wechatPay/?#/apply-manager-privilege\">点击马上体验>></a>\n";
            logger.info("新用户关注，返回自动回复信息："+autoReply);*/
            
           /* String autoReply = "有品位的美食家您好，我们已恭候多时，希望您能喜欢这里。\n"
                    + "/玫瑰 东海野生海鲜，船上鲜货进社区 — <a href=\"http://www.xiangweihui.com/wechatPay/?#/direct-address\">点击立即体验</a>\n"
                    + "/玫瑰 新人有好礼 — <a href=\"http://www.xiangweihui.com/wechatPay/?#/apply-manager-privilege\">0.1元体验董事特权</a>，购物立享实惠\n";
           */

            String autoReply = "来啦！欢迎你！\n"
                    + "吃个水果，来片面包，休息休息~\n"
                    + "优质食材这里满满满满一箩筐！\n"
                    + "申请加入你的社区团购群，可加官方客服微信：haiger5\n"
                    + "社区邻里惠购团每周一、三、五早上九点准时开团哦~\n"
                    + "点击菜单“进入商城”，老家特产精选、雁臻直供好货，等着你！\n"
                    + "点击菜单“联系我们”，开始我们的合作吧~\n";
            logger.info("新用户关注，返回自动回复信息：" + autoReply);
            String openid = map.get("FromUserName"); //用户 openid
            String mpid = map.get("ToUserName");   //公众号原始 ID

            //普通文本消息
            TextMessage txtmsg = new TextMessage();
            txtmsg.setToUserName(openid);
            txtmsg.setFromUserName(mpid);
            txtmsg.setCreateTime(System.currentTimeMillis());
            txtmsg.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);

            txtmsg.setContent(autoReply);
            return MessageUtil.textMessageToXml(txtmsg);
        } else if (map.get("Event").equals(MessageUtil.EVENT_TYPE_CLICK)) {
            if (map.containsKey("EventKey")) {
                //点击菜单消息
                String openid = map.get("FromUserName"); //用户 openid
                String mpid = map.get("ToUserName");   //公众号原始 ID
                TextMessage txtmsg = new TextMessage();
                txtmsg.setToUserName(openid);
                txtmsg.setFromUserName(mpid);
                txtmsg.setCreateTime(System.currentTimeMillis());
                txtmsg.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
                if (map.get("EventKey").equals("phone")) {
                    String autoReply = "400-801-8015";
                    txtmsg.setContent(autoReply);
                    return MessageUtil.textMessageToXml(txtmsg);
                } else if (map.get("EventKey").equals("guide")) {
                    NewsMessage newmsg = new NewsMessage();
                    newmsg.setToUserName(openid);
                    newmsg.setFromUserName(mpid);
                    newmsg.setCreateTime(System.currentTimeMillis());
                    newmsg.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);
                    Article article1 = new Article();
                    article1.setDescription("2017 乡味汇会员新政"); //图文消息的描述
                    article1.setPicUrl("https://mmbiz.qlogo.cn/mmbiz_jpg/HjMQZ5yp0fopxoVGJv7ZKW3lFaa48WOaOXbONPfUA6icQwfNpu8hQ0DvugUDmbAR4aR5meqibibyOa2wxebeXd3zQ/0?wx_fmt=jpeg"); //图文消息图片地址
                    article1.setTitle("2017 乡味汇会员新政");  //图文消息标题
                    article1.setUrl("http://mp.weixin.qq.com/s?__biz=MzIwNjIzNjQwMw==&mid=2247488944&idx=1&sn=255a7756c54a49c70b7e1cb1736d68e2&chksm=9725ea2ca052633a44aea6a87a9ee8a4cd379b369aa50775917662ea23589f2394f57a7c6a8e#rd");  //图文 url 链接
                    Article article2 = new Article();
                    article2.setDescription("乡味汇，因“乡愁”而来......"); //图文消息的描述
                    article2.setPicUrl("https://mmbiz.qlogo.cn/mmbiz_jpg/HjMQZ5yp0fpX7KrFRp13l02XdsyeKKtOY6JzdoB0ibLDrTbIDKWibThc5MibCVHcMPibmOoibBuooDFQMyibcqYknGcA/0?wx_fmt=jpeg"); //图文消息图片地址
                    article2.setTitle("乡味汇，因“乡愁”而来......");  //图文消息标题
                    article2.setUrl("http://mp.weixin.qq.com/s?__biz=MzIwNjIzNjQwMw==&mid=2247488944&idx=2&sn=15c6f2811acec9172c539090abad82cd&chksm=9725ea2ca052633a10f2c7e14e27d1457428ff025080943dd602912988d70242ac12e7dd6464#rd");  //图文 url 链接
                    Article article3 = new Article();
                    article3.setDescription("一县一店 招商公告"); //图文消息的描述
                    article3.setPicUrl("https://mmbiz.qlogo.cn/mmbiz_jpg/HjMQZ5yp0fopxoVGJv7ZKW3lFaa48WOaEvRty9MMm2QBcboib0HUkwa4yORxmBHpBDBIZM9mLrZMauZ7iaCIQNcA/0?wx_fmt=jpeg"); //图文消息图片地址
                    article3.setTitle("一县一店 招商公告");  //图文消息标题
                    article3.setUrl("http://mp.weixin.qq.com/s?__biz=MzIwNjIzNjQwMw==&mid=2247488944&idx=3&sn=3e1219c45d9a4f5e107da79cd183fb46&chksm=9725ea2ca052633a3510700001adf6c3b03b3d7a5515449b647b1707c1dec5719c2817b5dc56#rd");  //图文 url 链接
                    Article article4 = new Article();
                    article4.setDescription("乡味代言人常见问题解答"); //图文消息的描述
                    article4.setPicUrl("https://mmbiz.qlogo.cn/mmbiz_jpg/HjMQZ5yp0fopxoVGJv7ZKW3lFaa48WOauBrXGegQxylQj9KZpJ2ZiaYxYyM1DVBibo2lYMPVcicoqstbKg80KUzUw/0?wx_fmt=jpeg"); //图文消息图片地址
                    article4.setTitle("乡味代言人常见问题解答");  //图文消息标题
                    article4.setUrl("http://mp.weixin.qq.com/s?__biz=MzIwNjIzNjQwMw==&mid=2247488944&idx=4&sn=e0b6524bc098438195b3e857fcc82a01&chksm=9725ea2ca052633aa52d7b83002b7214fd92cba554cb4994938f2255bb6c7161fcb561dd2f6d#rd");  //图文 url 链接
                    List<Article> list = new ArrayList<Article>();
                    list.add(article1);
                    list.add(article2);
                    list.add(article3);
                    list.add(article4);
                    newmsg.setArticleCount(list.size());
                    newmsg.setArticles(list);
                    return MessageUtil.newsMessageToXml(newmsg);
                }
            }
        }

        return "";
    }
}
